package com.hilaver.dzmis.web.servlet.order;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.basicinfo.BiDZColor;
import com.hilaver.dzmis.order.OrderColorTest;
import com.hilaver.dzmis.service.impl.OrderColorTestServiceImpl;

/**
 * Servlet implementation class for Servlet: OrderServlet
 * 
 */
public class OrderColorTestServlet extends
		com.hilaver.dzmis.web.servlet.AbstractBaseServlet implements
		javax.servlet.Servlet {
	static final long serialVersionUID = 1L;
	
	public final static int ACTION_GET_MAX_REFERENCE_INDEX = 10;
	
	private OrderColorTest orderColorTest;
	private OrderColorTestServiceImpl orderColorTestService;

	public OrderColorTestServlet() {
		super();
		this.orderColorTestService = new OrderColorTestServiceImpl();
		this.orderColorTestService.setRealFileDir(UPLOAD_TEMP_REAL_DIR);
		this.fullName = OrderColorTest.class.getName();
		this.simpleName = OrderColorTest.class.getSimpleName();
	}

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			String outputStr = execute(request);
			if(this.action == ACTION_EDIT){
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		this.setAction(request);
		this.orderColorTestService.setLocale((Locale) request.getSession().getAttribute(
				Constants.SESSION_LOCALE));
		super.setAcUser(request, this.orderColorTestService);
		String rtn = "";
		switch (this.action) {
		case ACTOIN_GET_ALL_PAGINATION: // "getAllPagination";

			break;
		case ACTION_EDIT: // "edit";
			this.fillObj(request);
			this.orderColorTestService.edit(this.orderColorTest);
			break;
		case ACTION_DELETE: // "delete";
			this.setId(request);
			rtn = this.orderColorTestService.delete(this.id);
			break;
		case ACTION_GET: // "get one"
			this.setId(request);
			rtn = this.orderColorTestService.get(this.id);
			break;
		case ACTION_GET_WITH_DESCRIPTION:
			this.setId(request);
			rtn = this.orderColorTestService.get(this.id);
			break;
		case ACTION_GET_ALL_PAGINATION_FILTER: // "getAllPagination filter"
			this.setFilterParam(request);
			this.setPaginationParam(request);
			rtn = this.orderColorTestService.getAllPagination(page, sort, dir, filters);
			break;
		case ACTION_GET_ALL:
			this.setId(request);
			rtn = this.orderColorTestService.getAll(this.id);
			break;
		case ACTION_GET_MAX_REFERENCE_INDEX:
			rtn = this.orderColorTestService.getMaxReferenceIndex();
			break;
		}
		return rtn;
	}

	protected void fillObj(HttpServletRequest request) throws Exception {
		super.fillObj(request);
		this.orderColorTest = (OrderColorTest)this.obj;
		String dzColorIdStr = (String)getValue(request, "dzColorId");
		if(dzColorIdStr != null){
			Integer dzColorId = Integer.parseInt(dzColorIdStr);
			BiDZColor dzColor = new BiDZColor();
			dzColor.setId(dzColorId);
			this.orderColorTest.setBiDZColor(dzColor);
		}
	}
	
}